#include "SessionViewer.h"
#include "SessionPanel.h"
#include "FileTransferPanel.h"

int ID_SESSION_VIEWER = wxNewId();

int ID_SESSION_FILE_VIEWER = wxNewId();


BEGIN_EVENT_TABLE(SessionViewer, wxRichTextCtrl)

	EVT_MENU(ID_SESSION_FILE_VIEWER, SessionViewer::OnFileTransfer)

	//EVT_CONTEXT_MENU()
END_EVENT_TABLE()

SessionViewer::SessionViewer(wxWindow* parent)
:wxRichTextCtrl(parent, ID_SESSION_VIEWER, wxEmptyString, wxDefaultPosition,  wxDefaultSize,
                                wxVSCROLL | wxHSCROLL | wxTE_READONLY)
{
	this->Connect(wxEVT_KEY_DOWN, wxKeyEventHandler(SessionViewer::OnKeyDown), NULL, this); 
	this->Connect(wxEVT_CONTEXT_MENU, wxContextMenuEventHandler(SessionViewer::OnContextMenu), NULL, this); 

	menu = NULL;

}

SessionViewer::~SessionViewer()
{
	wxDELETE(menu);
}

void SessionViewer::OnContextMenu(wxContextMenuEvent& event)
{
	wxDELETE(menu);

	menu = new wxMenu;

 	menu->Append(wxID_SELECTALL, _("Select All"));
    menu->Append(wxID_COPY, _("Copy"));

	menu->AppendSeparator();

	menu->AppendCheckItem(ID_SESSION_FILE_VIEWER, _("File Transfer Panel"));   

	if(sessionPanel->ftPanel->IsShown()){
		menu->Check(ID_SESSION_FILE_VIEWER, true);
	}else{
		menu->Check(ID_SESSION_FILE_VIEWER, false);
	}

    PopupMenu(menu);
}

void SessionViewer::OnKeyDown(wxKeyEvent& event)
{
	const wxChar ch = event.GetKeyCode();


	if(event.ControlDown() && ch == 'A'){
		DEBUG(_T("ctrl-a"));
		this->SetSelection(-1, -1);
		return;
	}

	if(event.ControlDown() && ch == 'C'){
		DEBUG(_T("ctrl-c"));
		this->Copy();
		return;
	}

	event.Skip();
}

void SessionViewer::OnFileTransfer(wxCommandEvent& event)
{

	if(menu->IsChecked(ID_SESSION_FILE_VIEWER)){
		menu->Check(ID_SESSION_FILE_VIEWER, false);
		sessionPanel->ShowFileTransferPanel(false);
	}else{
		menu->Check(ID_SESSION_FILE_VIEWER, true);
		sessionPanel->ShowFileTransferPanel(true);
	}
}
